import axios from 'axios'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Router from 'vue-router'

Vue.use(Router)

const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect:'/home',
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('@/components/user/home')
        },
        {
            path: '/archive/:archiveId(\\d+)',
            name: 'archive',
            component: () => import('@/components/archive/archive-view.vue'),
            props: true,
        },
        {
            path: '/archive/:archiveId(\\d+)/doc/:docId(\\d+)',
            name: 'doc',
            component: () => import('@/components/archive/doc-view.vue'),
            props: true
        },
        {
            path: '/archive/:archiveId(\\d+)/doc/:docId(\\d+)/page',
            redirect:'/archive/:archiveId(\\d+)/doc/:docId(\\d+)/page/edit',
            component: () => import('@/components/doc/docHome.vue'),
            props: true,
            children: [
                {
                    path: '/archive/:archiveId(\\d+)/doc/:docId(\\d+)/page/edit',
                    component: () => import('@/components/doc/docEditor.vue'),
                    props: true,
                },
                {
                    path: '/archive/:archiveId(\\d+)/doc/:docId(\\d+)/page/preview',
                    component: () => import('@/components/doc/docPreview'),
                    props: true,
                }
            ]

        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/components/login/SimpleLogin.vue'),
            props: true
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/components/login/Register.vue'),
            props: true
        },
        {
            path: '/forgetpwd',
            name: 'forgetpwd',
            component: () => import('@/components/login/ForgetPwd.vue'),
            props: true
        },
    ]
});
router.beforeEach((to, from, next) => {
    // 判断是否需要进行提醒确认
    if (to.meta.confirmMessage) {
      // 弹出确认对话框
      if (confirm(to.meta.confirmMessage)) {
        // 用户确认，允许进行页面切换
        next();
      } else {
        // 用户取消，阻止页面切换
        next(false);
      }
    } else {
      // 不需要提醒确认，直接进行页面切换
      next();
    }
  });

/*
router.beforeEach((to, from, next) =>{
    if(to.path.startsWith('/login')) {
        window.sessionStorage.removeItem('access-admin')
        next()
    } else {
        let token = window.sessionStorage.getItem('access-admin')
        if(!admin) {
            next('/login')
        } else {
            axios({
                url:'',
                method:'get',
                headers:{
                    AUTHORIZATION : token
                }
            }).then((response) => {
                if(!response.data){
                    console.log('路由守卫：校验失败')
                    next('/error/loginInfoInvalid')
                }
            })
            next()
        }
    }

})

meta:{
                        confirmMessage:'离开界面后未保存的编辑内容将消失，确定要切换界面吗?'
                    }
*/
export default router